import com.googlecode.scup.parser.Parser;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.*;

    /*
    <applet code="SLR" width=600 height=300>
    </applet>
     */
public class Main extends Applet implements ActionListener {
        private Button button1 = null;
        private Button button2 = null;
        private TextArea grammarBox = null;
        private TextArea parsingBox = null;

        private Parser parser = null;

        /*public static void main (String args[]) {

        }*/

        public void init() {
            button1 = new Button("Read grammar");
            button2 = new Button("Do match");
            grammarBox = new TextArea(firstGrammar(), 15, 35);
            parsingBox = new TextArea(15, 35);

            add(grammarBox);
            add(parsingBox);
            add(button1);
            add(button2);

            button1.addActionListener(this);
            button2.addActionListener(this);
        }

        public void start() {

        }

        public void paint(Graphics graph) {

        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            String str = actionEvent.getActionCommand();
            if (str == "Read grammar") {
                try {
                    parser = new Parser(grammarBox.getText().toCharArray());
                    grammarBox.setBackground(Color.WHITE);
                } catch (Exception ex) {
                    grammarBox.setBackground(Color.RED);
                }
            } else {
                if (parser.match(parsingBox.getText())) {
                    parsingBox.setBackground(Color.GREEN);
                } else {
                    parsingBox.setBackground(Color.RED);
                }
            }
        }

        private String firstGrammar() {
            String result = "E -> E + T | E - T | T\n" +
                    "T -> T * F | T / F | F\n" +
                    "F -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | ( E )";
            return result;
        }
    }
